package org.zhxy.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zhxy.zhxy.dao.AppointRecordDao;
import org.zhxy.zhxy.dao.ClassroomDao;
import org.zhxy.zhxy.dao.NoticeDao;
import org.zhxy.zhxy.dao.RoomCommentDao;
import org.zhxy.zhxy.entity.AppointRecord;
import org.zhxy.zhxy.entity.Classroom;
import org.zhxy.zhxy.entity.Notice;
import org.zhxy.zhxy.entity.RoomComment;
import org.zhxy.zhxy.entity.dto.AddRoomCommentDto;
import org.zhxy.zhxy.service.NoticeService;
import org.zhxy.zhxy.service.RoomCommentService;
import org.zhxy.zhxy.utils.UserHolder;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class RoomCommentServiceImpl  extends ServiceImpl<RoomCommentDao, RoomComment> implements RoomCommentService {

    @Resource
    private RoomCommentDao roomCommentDao;
    @Resource
    private ClassroomDao classroomDao;
    @Resource
    private AppointRecordDao appointRecordDao;
    @Override
    public List<RoomComment> getRoomComment(Integer roomId) {
        return roomCommentDao.getRoomCommentByRoomId(roomId);

    }

    @Override
    @Transactional
    public void addRoomComment(AddRoomCommentDto addRoomCommentDto) {
        String rooId = addRoomCommentDto.getRoomId();
        QueryWrapper<Classroom> classroomQueryWrapper = new QueryWrapper<>();
        classroomQueryWrapper.eq("id",rooId);
        Classroom classroom = classroomDao.selectOne(classroomQueryWrapper);
        if (classroom==null){
            throw new RuntimeException("教室不存在");
        }
        String recordId = addRoomCommentDto.getRecordId();
        UpdateWrapper<AppointRecord> appointRecordUpdateWrapper = new UpdateWrapper<AppointRecord>();
        appointRecordUpdateWrapper.eq("id",recordId);
        appointRecordUpdateWrapper.set("status",4);
        appointRecordDao.update(null,appointRecordUpdateWrapper);
        RoomComment roomComment = new RoomComment();
        roomComment.setUserId(UserHolder.getUser().getId());
        roomComment.setRoomId(Integer.parseInt(addRoomCommentDto.getRoomId()));
        roomComment.setComment(addRoomCommentDto.getContent());
        roomComment.setCreateTime(new Date());
        roomComment.setUpdateTime(new Date());
        int insert = roomCommentDao.insert(roomComment);
    }
}
